<?php
/**
  * CLOUD
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
class View
{
	private static $object;
	
	/**
	 * smarty case
	 * @var object
	 */
	public $smarty;
	
	public function __construct( $config )
	{
		$systemConfig = Common::getConfig( 'System' );
		include_once LIB . "smarty/Smarty.class.php";
		$this->smarty = new Smarty();
		$this->smarty->caching = $config['caching'] ? $config['caching'] : false;

		$this->smarty->cache_lifetime = $config['cache_lifetime'] ? $config['cache_lifetime'] : 86400;
		
		$this->smarty->template_dir = $config['template_dir'] ? $config['template_dir'] : TPL . STYLE ;
		
		$this->smarty->compile_dir = $config['compile_dir'] ? $config['compile_dir'] : TPLCOMPILE_DIR;
		
		$this->smarty->cache_dir = $config['cache_dir'] ? $config['cache_dir'] : TPLCACHE_DIR;
		
		$this->smarty->left_delimiter = $config['left_delimiter'] ? $config['left_delimiter'] : "<!--{";
		
		$this->smarty->right_delimiter = $config['right_delimiter'] ? $config['right_delimiter'] : "}-->";
	}
	
	/**
	 * 实例
	 * @param array $config
	 * @return View
	 */
	public static function getInstance( $config = array() )
	{
		if ( !is_object( self::$object ) )
		{
			self::$object = new View( $config );
		}
		return self::$object;
	}
	
	/**
	 * 获取smarty实例
	 */
	public function getSmarty()
	{
		return $this->smarty;
	}
	
	/**
	 * 给变量赋值
	 * @param string|array 		$keys
	 * @param string|array|int	$vals
	 * @throws BaseException
	 */
	public function tplAssign( $keys = '' , $vals = '' )
	{
		if ( $keys && $vals )
		{
			if ( is_array( $keys ) && is_array( $vals ) )
			{
				if ( count( $keys ) != count( $vals ) )
				{
					throw new BaseException( BaseException::SYS_ERR_TPLASSIGN_NUM );
				}
				foreach( $vals as $key => $val )
		 		{
		 			$this->smarty->assign( $keys[$key] , $vals[$key] );
		 		}
			}
			else
			{
				$this->smarty->assign( $keys , $vals );
			}
		}
		
		$this->tplDefinedAssign();
	}
	
	/**
	 * display page
	 * 
	 * @param int $template
	 * @param int $cacheId
	 */
	public function display( $template , $cacheId = null )
	{
		$this->smarty->display( TPL . STYLE . '/' . $template , $cacheId );
	}
	
	/**
	 * define assign
	 */
	public function tplDefinedAssign()
	{
		$this->smarty->assign( 'SITEURL' , SITEURL );
		$this->smarty->assign( 'SITENAME' , Common::getConfig( 'System' , 'site_name' ) );
		$this->smarty->assign( 'CSSURL' , CSSURL );
		$this->smarty->assign( 'JSURL' , JSURL );
		$this->smarty->assign( 'IMAGEURL' , IMAGEURL );
		$this->smarty->assign( 'ATTACHMENTURL' , ATTACHMENTURL );
		$this->smarty->assign( 'c' , PARAM_C );
		$this->smarty->assign( 'a' , PARAM_A );
		$this->smarty->assign( 'rand' , MICTOTIME );
	}
	
	/**
	 * clean template compile file
	 * 
	 * @param string $fileName
	 */
	public static function cleanTplCompile( $fileName = '' )
	{
		if ( $dh = opendir( TPLCOMPILE_DIR ) )
		{
			while ( ( $file = readdir( $dh ) ) !== false )
			{
				if ( $file != '.' && $file != '..' && $file != 'index.htm' )
				{
					if ( empty( $fileName ) )
					{
						@unlink( TPLCOMPILE_DIR . $file );
					}
					elseif ( $fileName && substr( $file , -15 , 11 ) == $fileName )
					{
						@unlink( TPLCOMPILE_DIR . $file );
						break;
					}
				}
			}
			closedir( $dh );
		}
	}
	
	/**
	 * clean template cache file
	 */
	public static function cleanTplCache()
	{
		if ( $dh = opendir( TPLCACHE_DIR ) )
		{
			while ( ( $file = readdir( $dh ) ) !== false )
			{
				if ( $file != '.' && $file != '..' && $file != 'index.htm' )
				{
					@unlink( TPLCOMPILE_DIR . $file );
				}
			}
			closedir( $dh );
		}
	}
	
}